package com.itheima.servlet;

import com.itheima.bean.User;
import com.itheima.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class ClearCookieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("content-type", "text/html;charset=utf-8");
		Cookie cookies[] = request.getCookies();
		Cookie co = null;
		//判断cookies是否为null
		if(cookies!=null){
			//从cookies中查找与usernameAndPwd相匹配的cookie
			for(Cookie cookie:cookies){
				if("usernameAndPwd".equals(cookie.getName())){
					System.out.println(cookie.getName()+":"+cookie.getValue());
					cookie.setMaxAge(0);
					//把cookie写回浏览器
					response.addCookie(cookie);

					//等待3s刷新登录后的首页
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					//重定向到首页
					response.sendRedirect(request.getContextPath()+"/index.jsp");
					return;
				}
			}
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			response.sendRedirect(request.getContextPath()+"/index.jsp");
			return;
		}else {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			response.sendRedirect(request.getContextPath()+"/index.jsp");
			return;
		}




	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
